package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.DateUtil;
import com.cxf.windex.biz.CxfAccountLogBiz;
import com.cxf.windex.biz.CxfUserInfoBiz;
import com.cxf.windex.entity.CxfAccountLog;
import com.cxf.windex.entity.CxfUserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: drp
 * @description: 资金记录
 * @author: chenxf
 * @create: 2018-05-24 10:44
 **/
@Api(tags = {"聚能小宝 - 支付詳情接口"})
@Controller
@RequestMapping("api_account_log")
public class CxfAccountLogController extends BaseRestController<CxfAccountLogBiz,CxfAccountLog> {

    @Autowired
    private CxfUserInfoBiz cxfUserInfoBiz;

    @ApiOperation(value = "查询收入支出日志", notes = "查询收入支出日志")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "orderStr", value = "排序 desc", required = false, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "queryType", value = "查询类型 默认 1收入 0支出", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/szList", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<CxfAccountLog> szList(
        @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
        @RequestParam(defaultValue = "desc") String orderStr,  @RequestParam(defaultValue = "1")  int queryType) {

        String userId = this.getCurrentUserId();

        if ( userId==null || userId.equals("") ) {
            return new TableResultResponse<CxfAccountLog>(0, offset, limit,new ArrayList<CxfAccountLog>());

        }
        Example example = new Example(CxfAccountLog.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(userId)) {
            criteria.andEqualTo("userid",  userId );
        }

        if(queryType==1){
            criteria.andGreaterThan("money","0");
        }else{
            criteria.andLessThan("money","0");
        }

        example.setOrderByClause("crt_time "+ orderStr);
        PageHelper.startPage(offset, limit);
        List<CxfAccountLog> items = baseBiz.selectByExample(example);

        PageInfo<CxfAccountLog> pageInfo = new PageInfo<CxfAccountLog>(items);
        return new TableResultResponse<CxfAccountLog>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
            offset, limit,
            items);

    }

    @ApiOperation(value = "查询日志", notes = "查询日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "orderStr", value = "排序 desc", required = false, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<CxfAccountLog> list(
            @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
            @RequestParam(defaultValue = "desc") String orderStr, String beginTime, String endTime, String type,String status) {

        String userId = this.getCurrentUserId();
        String userType=this.getCurrentUserType();

        if ( userId==null || userId.equals("") ) {
            return new TableResultResponse<CxfAccountLog>(0, offset, limit,new ArrayList<CxfAccountLog>());

        }
        Example example = new Example(CxfAccountLog.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(userId)&&!userType.equals("4")) {
            criteria.andEqualTo("userid",  userId );
        }

        if(StringUtils.isNotBlank(type)){
            criteria.andLike("operateType","%" + type + "%");
        }
        if(StringUtils.isNotBlank(status)){
            criteria.andEqualTo("status",status);
        }

        if (StringUtils.isNotBlank(beginTime)) {
            criteria.andGreaterThanOrEqualTo("crtTime", DateUtil.getDate(beginTime));
        }

        if (StringUtils.isNotBlank(endTime)) {
            criteria.andLessThanOrEqualTo("crtTime",  DateUtil.getDate(endTime) );
        }
        example.setOrderByClause("crt_time "+ orderStr);
        PageHelper.startPage(offset, limit);
        List<CxfAccountLog> items = baseBiz.selectByExample(example);

        PageInfo<CxfAccountLog> pageInfo = new PageInfo<CxfAccountLog>(items);
        return new TableResultResponse<CxfAccountLog>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
                offset, limit,
                items);

    }


    @ApiOperation(value = "查询日志", notes = "查询日志")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "orderStr", value = "排序 desc", required = false, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/rank", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<CxfAccountLog> rank(
        @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
        @RequestParam(defaultValue = "desc") String orderStr, String beginTime, String endTime, String yearMonth) {

        if (StringUtils.isBlank(yearMonth)) {
            yearMonth = DateUtil.getYearMonth();
        }
        PageHelper.startPage(offset, limit);
        List<CxfAccountLog> items = baseBiz.selectRank(yearMonth);
        PageInfo<CxfAccountLog> pageInfo = new PageInfo<CxfAccountLog>(items);
        return new TableResultResponse<CxfAccountLog>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
            offset, limit,
            items);

    }

    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "记录数", required = true, paramType = "query", dataType = "long"),
    })
    @RequestMapping(value = "/txInfo", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse txInfo(Long id) {
        CxfAccountLog cxfAccountLog=baseBiz.selectById(id);
        if (cxfAccountLog==null) {
            return new ObjectRestResponse().rel(false).msg("找不到对应的提现信息");
        }
        if(cxfAccountLog.getStatus().equals("1")){
            return new ObjectRestResponse().rel(false).msg("提现已处理");
        }

        String alipay="暂无支付宝帐户，无法支付";

        CxfUserInfo queryEntity=new CxfUserInfo();
        queryEntity.setUserid(cxfAccountLog.getUserid());
        queryEntity=cxfUserInfoBiz.selectOne(queryEntity);
        if(queryEntity!=null && StringUtils.isNotBlank(queryEntity.getAlipay())){
            alipay=queryEntity.getAlipay();
        }

        resultMap.put("money",cxfAccountLog.getMoney());
        resultMap.put("name",cxfAccountLog.getName());
        resultMap.put("id",id);
        resultMap.put("alipay",alipay);

        return new ObjectRestResponse().rel(true).msg("查询成功").result(resultMap);

    }
}
